package controller.article;

import domain.User;
import service.article.ArticleService;
import service.article.ArticleServiceImpl;
import service.user.UserService;
import service.user.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CreatNews extends HttpServlet {
    private static final long serialVersionUID = 1L;
    ArticleService articleService = new ArticleServiceImpl();
    public CreatNews() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String title = request.getParameter("title");
        String abs = request.getParameter("abs");
        int type = Integer.parseInt(request.getParameter("type"));
        int state = Integer.parseInt(request.getParameter("state"));
        String info = request.getParameter("news_content");
        if(request.getSession().getAttribute("userloginflag")==null){
            response.sendRedirect(request.getContextPath() + "/admin/page/404/404.html");
        }else {
            User user = (User) request.getSession().getAttribute("userloginflag");
            String author = user.getName();

            int i = articleService.creatArticle(type, title, info, abs, author, state);
            if (i > 0) {

                response.sendRedirect(request.getContextPath() + "/admin/page/news/newsAdd.jsp?msg=1");
            } else {
                response.sendRedirect(request.getContextPath() + "/admin/page/news/newsAdd.jsp?msg=0");
            }
        }
    }
}